package com.sysman.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.sysman.HttpTools.DataAnalyze;
import com.sysman.HttpTools.DataAnalyzeDetails;
import com.sysman.HttpTools.HttpRequest;
import com.sysman.po.DoctorinfoCustom;
import com.sysman.po.Doctorlogininfo;
import com.sysman.po.DoctorlogininfoCustom;
import com.sysman.service.DoctorService;

/** 
* @author  LTT-AH-ZY E-mail: 
* @date 创建时间：2017年8月8日 上午10:31:17 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/Doctor")
public class DoctorController {
	/**
	 * 注入DoctorService服务，实现对dao层的数据库操作，实现列表查询
	 */
	@Autowired
	private DoctorService doctorService;
	/**
	 * 这里是医生端列表初始化界面，实现对所有医生端注册用户（包括正式用户和非正式用户）的管理
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/doctorlist")
	public ModelAndView doctorslist()throws Exception{
		//List<DoctorlogininfoCustom> DoctorlogininfoList = doctorService.InitDoctorlogininfoList(null);
		ModelAndView model = new ModelAndView();
		//model.addObject("DoctorlogininfoList", DoctorlogininfoList);
		model.setViewName("View/DoctorMan/DoctorList");
		return model;
	}
	/**
	 * 医生端列表动态加载
	 * @param limit
	 * @param offset
	 * @param type
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/doctorListAjax")
	public @ResponseBody Map<String,Object> doctorListAjax(Integer limit,Integer offset,Integer type,String doctitle,Integer docage,String docloginphone,String dochosplevel,Integer dochospfdept,Integer dochospsdept,String startdatetime,String enddatetime,HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		if(docage==null) {
			docage=0;
		}
		//doctitle=new String(doctitle.getBytes("iso8859-1"),"utf-8");
		System.out.println("输出接收的参数是："+limit+"//"+offset+"//"+type+"//"+adminloginid);
		String DoctorParam = "limit="+limit+"&offset="+offset+"&type="+type+"&adminloginid="+adminloginid+"&doctitle="+doctitle+"&docage="+docage+"&docloginphone="+docloginphone+"&dochosplevel="+dochosplevel+"&dochospfdept="+dochospfdept+"&dochospsdept="+dochospsdept+"&startdatetime="+startdatetime+"&enddatetime="+enddatetime;//参数串
		System.out.println("输出这个参数串："+DoctorParam);
		List<Map<String,Object>> jsonlist=new ArrayList<>();
		Integer total=0;
		String jsonobjectmsg="";
		String jsonobjectcode="";
		Map<String, Object> map = new HashMap<String, Object>();  
		if(adminloginid==null){
	        jsonobjectmsg = "请先登录！";
			jsonobjectcode ="-100";
		}else {
			try {
				JSONObject DoctorListjson = HttpRequest.sendPost("listdoctor", DoctorParam);
				System.out.println("输出接收到的json数据是："+DoctorListjson);
				
				jsonobjectmsg = DoctorListjson.get("msg").toString();
				jsonobjectcode =DoctorListjson.get("code").toString();
				if("100".equals(jsonobjectcode)) {
					try {
						if(DoctorListjson.get("data").toString()!="null") {
							JSONObject jsonobjectdata =DoctorListjson.getJSONObject("data");
							JSONArray jsonarrayrows = jsonobjectdata.getJSONArray("rows");
							total = Integer.parseInt(jsonobjectdata.get("total").toString());
							jsonlist = DataAnalyze.DoctorJsonToList(jsonarrayrows);
						}
					} catch (Exception e) {
						System.out.println("出错信息是："+e.getMessage());
						jsonobjectmsg = "数据解析出错！";
						jsonobjectcode ="-300";
					}
				}
			} catch (Exception e) {
				System.out.println("出错信息是："+e.getMessage());
				jsonobjectmsg = "数据传输错误！";
				jsonobjectcode ="-200";
				
			}
		}
		map.put("msg", jsonobjectmsg);
		map.put("rows", jsonlist);
		map.put("code", jsonobjectcode);
        map.put("total", total);
        
		System.out.println("输出这个map："+map);
		return map;
	}
	/**
	 * 这里是医生端审核初始化界面，实现对所有医生端的符合条件的用户实现审核与否管理，（条件是：为未审核用户（即非正式用户），且未详细注册用户）
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/doctorexamine")
	public ModelAndView doctorexamine() throws Exception {
		//List<DoctorlogininfoCustom> doctorlogininfoExamineList = doctorService.InitDoctorlogininfoExamineList(null);
		ModelAndView model = new ModelAndView();
		//model.addObject("doctorlogininfoExamineList", doctorlogininfoExamineList);
		model.setViewName("View/DoctorMan/DoctorExamine");
		return model;
	}
	/**
	 * 类处理对医生审核界面的审核提交，实现了多id的循环提交即批量审核
	 * @param doctorloginids
	 * @param doctorlogininfoCustom
	 * @return
	 */
	@RequestMapping(value="/doctorexamineSubmit")
	public @ResponseBody Map<String,Object> doctorexamineSubmit(HttpServletRequest request) throws Exception{
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		
		String docloginid = request.getParameter("docloginid");
		String isagree = request.getParameter("isagree");
		boolean type = Boolean.parseBoolean(isagree);
		String idea = request.getParameter("idea");
		String Param = "adminloginid="+adminloginid+"&docloginid="+docloginid+"&type="+type+"&idea="+idea;
		System.out.println("参数Param是："+Param);
		Map<String, Object> ExamineJson = new HashMap<>();
		String code="";
		String msg="";
		if(adminloginid!=null && docloginid!=null && isagree!=null){
			try {
				JSONObject json = HttpRequest.sendPost("checkdoctor",Param);
				System.out.println("服务器返回json信息："+json);
		        code = json.get("code").toString();
		        msg = json.get("msg").toString();
			} catch (Exception e) {
				code = "-200";
		        msg = "数据传输错误";
			}
		}else {
			code = "-100";
	        msg = "提交审核失败";
		}
		ExamineJson.put("code", code);
		ExamineJson.put("msg", msg);
		return ExamineJson;
	}
	/**
	 * 对doctorlogininfo表的信息查询，
	 * @param id
	 * @param doctorinfoCustom
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/doctordetails")
	public ModelAndView doctordetails(@RequestParam(value = "id", required = true) Integer id,DoctorinfoCustom doctorinfoCustom) throws Exception {
		Doctorlogininfo doctorlogininfodetails = doctorService.findDoctorlogininfoDetails(id);
		doctorinfoCustom.setDocloginid(id);
		DoctorinfoCustom doctorinfodetails = doctorService.findDoctorinfoDetails(doctorinfoCustom);
		ModelAndView model = new ModelAndView();
		model.addObject("doctorlogininfodetails", doctorlogininfodetails);
		model.addObject("doctorinfodetails", doctorinfodetails);
		model.setViewName("View/DoctorMan/DoctorDetails");
		return model;
	}
	/**
	 * 根据提供的医生的登陆id，和管理员登陆的adminloginid，获取医生的详细信息，docloginid和adminloginid不可为空！
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getDoctorDetails")
	public @ResponseBody Map<String,Object> getDoctorDetails(HttpServletRequest request)throws Exception{
		HttpSession session=request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		
		String docloginid = request.getParameter("docloginid");
		String doctorparam = "adminloginid="+adminloginid+"&docloginid="+docloginid;
		Map<String,Object> jsontomap  =new HashMap<String,Object>();
		Map<String, Object> map = new HashMap<String, Object>(); 
		String code="";
		String msg="";
		if(docloginid==null || adminloginid==null){
			code="-100";
			msg="请先登录";
		}else {
			try {
				JSONObject DoctorDeatilsjson = HttpRequest.sendPost("getdoctordetail", doctorparam);
				System.out.println("输出接收到的json数据是："+DoctorDeatilsjson);
				
				msg = DoctorDeatilsjson.get("msg").toString();
				code =DoctorDeatilsjson.get("code").toString();
				try {
					jsontomap = DataAnalyzeDetails.DoctorJsonToDetails(DoctorDeatilsjson.getJSONObject("data"));
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
					msg="数据解析错误";
					code="-300";
				}
			} catch (Exception e) {
				System.out.println("出错信息是："+e.getMessage());
				msg="数据请求错误";
				code="-200";
			}
		}
		map.put("msg", msg);
		map.put("code", code);
		map.put("details", jsontomap);
		return map;
	}
	/**
	 * 管理员端获取医生职称列表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/loaddoctitile")
	public @ResponseBody Map<String,Object> loaddoctitile(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		
		Map<String,Object> result = new HashMap<String,Object>();
		List<Map<String,Object>> list = new ArrayList<>();
		String code="";
		String msg="";
		try {
			JSONObject titlejson = HttpRequest.sendPost("listdoctortitle", "");
			code = titlejson.get("code").toString();
			msg = titlejson.get("msg").toString();
			if("100".equals(code)) {
				try {
					JSONArray data = titlejson.getJSONArray("data");
					for(int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject job = data.getJSONObject(i);
						map.put("doctitleid", job.get("doctitleid").toString());
						map.put("doctitlename", job.get("doctitlename").toString());
						list.add(map);
					}
				} catch (Exception e) {
					code="-200";
					msg="数据解析错误";
					System.out.println("出错信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		result.put("list", list);
		
		return result;
	}
	/**
	 * 获取医院等级
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/loadhosplevel")
	public @ResponseBody Map<String,Object> loadhosplevel()throws Exception{
		Map<String,Object> result = new HashMap<String,Object>();
		List<Map<String,Object>> list = new ArrayList<>();
		String code="";
		String msg="";
		try {
			JSONObject leveljson = HttpRequest.sendPost("listhosplevel", "");
			code = leveljson.get("code").toString();
			msg = leveljson.get("msg").toString();
			if("100".equals(code)) {
				try {
					JSONArray data = leveljson.getJSONArray("data");
					for(int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject job = data.getJSONObject(i);
						map.put("hosplevelid", job.get("hosplevelid").toString());
						map.put("hosplevelname", job.get("hosplevelname").toString());
						list.add(map);
					}
				} catch (Exception e) {
					code="-200";
					msg="数据解析错误";
					System.out.println("出错信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		result.put("list", list);
		
		return result;
	}
	/**
	 * 根据父ID获取医院科室信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/loadhospdept")
	public @ResponseBody Map<String,Object> loadhospdept(HttpServletRequest request)throws Exception{
		Integer hospdeptfatherid =Integer.parseInt(request.getParameter("hospdeptfatherid"));
		String param = "hospdeptfatherid="+hospdeptfatherid;
		System.out.println("输出参数串param："+param);
		Map<String,Object> Deptmap = new HashMap<String,Object>();
		List<Map<String,Object>> list = new ArrayList<>();
		String code="";
		String msg="";
		try {
			JSONObject deptjson = HttpRequest.sendPost("listalldept", param);
			code = deptjson.get("code").toString();
			msg = deptjson.get("msg").toString();
			if("100".equals(code)) {
				try {
					JSONArray data = deptjson.getJSONArray("data");
					for(int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject job = data.getJSONObject(i);
						map.put("hospdeptid", job.get("hospdeptid").toString());
						map.put("hospdeptname", job.get("hospdeptname").toString());
						list.add(map);
					}
				} catch (Exception e) {
					code="-200";
					msg="数据解析错误";
					System.out.println("出错信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错信息是："+e.getMessage());
		}
		Deptmap.put("code", code);
		Deptmap.put("msg", msg);
		Deptmap.put("list", list);
		
		return Deptmap;
	}
	/**
	 * 根据一级部门名获取二级部门
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/Eloadhospdept")
	public @ResponseBody Map<String,Object> Eloadhospdept(HttpServletRequest request)throws Exception{
		String hospdeptfathername =request.getParameter("hospdeptfathername");
		String param = "hospdeptfathername="+hospdeptfathername;
		System.out.println("输出参数串param："+param);
		Map<String,Object> Deptmap = new HashMap<String,Object>();
		List<Map<String,Object>> list = new ArrayList<>();
		String code="";
		String msg="";
		try {
			JSONObject deptjson = HttpRequest.sendPost("", param);
			code = deptjson.get("code").toString();
			msg = deptjson.get("msg").toString();
			if("100".equals(code)) {
				try {
					JSONArray data = deptjson.getJSONArray("data");
					for(int i=0;i<data.length();i++) {
						Map<String,Object> map = new HashMap<>();
						JSONObject job = data.getJSONObject(i);
						map.put("hospdeptid", job.get("hospdeptid").toString());
						map.put("hospdeptname", job.get("hospdeptname").toString());
						list.add(map);
					}
				} catch (Exception e) {
					code="-200";
					msg="数据解析错误";
					System.out.println("出错信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错信息是："+e.getMessage());
		}
		Deptmap.put("code", code);
		Deptmap.put("msg", msg);
		Deptmap.put("list", list);
		
		return Deptmap;
	}
	/**
	 * 修改医生信息提交
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/doctorinfoSubmit")
	public @ResponseBody Map<String,Object> doctorinfoSubmit(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid=(Integer)session.getAttribute("adminloginid");
		String docname=request.getParameter("docname");
		String docmale=request.getParameter("docmale");
		String docage=request.getParameter("docage");
		String doctitle=request.getParameter("doctitle");
		String doccardnum=request.getParameter("doccardnum");
		String dochosp=request.getParameter("dochosp");
		String hosplevel=request.getParameter("hosplevel");
		String docprimarydept=request.getParameter("docprimarydept");
		String docseconddept=request.getParameter("docseconddept");
		String dochospprovince=request.getParameter("dochospprovince");
		String dochospcity=request.getParameter("dochospcity");
		String dochosparea=request.getParameter("dochosparea");
		String dochospother=request.getParameter("dochospother");
		String dochosplon=request.getParameter("dochosplon");
		String dochosplat=request.getParameter("dochosplat");
		String param="adminloginid="+adminloginid+"&docname="+docname+"&docmale="+docmale+"&docage="+docage+"&doctitle="+doctitle+"&doccardnum="+doccardnum+
		    	"&dochosp="+dochosp+"&hosplevel="+hosplevel+"&docprimarydept="+docprimarydept+"&docseconddept="+docseconddept+
		    	"&dochospprovince"+dochospprovince+"&dochospcity="+dochospcity+"&dochosparea="+dochosparea+"&dochospother="+dochospother+
		    	"&dochosplon="+dochosplon+"&dochosplat="+dochosplat;
		Map<String,Object> result = new HashMap<String,Object>();
		String code="";
		String msg="";
		try {
			JSONObject json = HttpRequest.sendPost("editdoctorinfo", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
			
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		result.put("code", code);
		result.put("msg", msg);
		
		return result;
	}
	
}
